package com.logic.landseaserver.ws;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.service.IContractOfflineService;
import com.logic.landseaserver.ws.request.CreateContractUrgentReq;
import com.logic.landseaserver.ws.request.CreateOffLineContractsReq;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * Created by jl on 2017/1/9.
 */
@Path("/contractOffLine")
@Api(value = "线下合同Controller")
public class ContractOffLineRest {

    @Autowired
    IContractOfflineService contractService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @POST
    @Path("/createOffLineContract")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "新建一个线下合同", position = 1)
    public Response createOffLineContract(@ApiParam(value = "param", required = true) CreateOffLineContractsReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            map.setData(contractService.insert(req));
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createOffLineContract error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createOffLineContract error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/updateOffLineContract")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "修改线下合同", position = 1)
    public Response updateOffLineContract(@ApiParam(value = "param", required = true) CreateOffLineContractsReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            contractService.update(req);
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (BusinessException e) {
            logger.error("updateOffLineContract error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("updateOffLineContract error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/updateBaseInfo")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "修改线下合同基本信息", position = 1)
    public Response updateBaseInfo(@ApiParam(value = "param", required = true) CreateContractUrgentReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            contractService.updateContractBaseInfo(req);
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (BusinessException e) {
            logger.info("updateBaseInfo BusinessException: "+ e.getMessage());
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("updateBaseInfo Exception: ", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @DELETE
    @Path("/deleteRoommate/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "delete deposit by set deleted", position = 4)
    public Response deleteRoommate(@ApiParam(value = "param", required = true) @PathParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
            contractService.deleteContractRoommate(id);
            map.setResult(CommonResult.SUCCESS,"delete activity");
        } catch (Exception e) {
            logger.error("deleteRoommate Exception: ", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/updateAuthCode")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "合同编辑->修改授权码", position = 5)
    public Response updateAuthCode(@ApiParam(value = "param", required = true) CreateOffLineContractsReq req) {
        CommonResultMap map = new CommonResultMap();
        try {
            contractService.updateAuthCode(req);
            map.setResult(CommonResult.SUCCESS, "update success");
        } catch (BusinessException e) {
            logger.error("updateOffLineContract error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("updateOffLineContract error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

}

